import {
  customerPage,
  customerInsertOrUpdate,
  customerDel,
  selectList,
  userList,
} from '../services/customer';
import React from 'react';
import { Input, message, Tooltip } from 'antd';

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '客户名称',
    dataIndex: 'projectName',
    width: '200px',
    editable: true,
    ellipsis: true,
    render: (text, record, index) => {
      return (
        <Tooltip placement="top" title={text}>
          <span>{text}</span>
        </Tooltip>
      );
    },
  },
  {
    title: '项目简称',
    dataIndex: 'customerName',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '项目名称',
    dataIndex: 'projectShort',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '货物名称',
    dataIndex: 'goodsName',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '结算周期',
    dataIndex: 'paymentDays',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '回款周期',
    dataIndex: 'paymentPeriod',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '开票资料',
    dataIndex: 'invoiceInfo',
    width: '220px',
    editable: true,
    ellipsis: true,
    render: (text, record, index) => {
      return (
        <Tooltip placement="top" title={text}>
          <span>{text}</span>
        </Tooltip>
      );
    },
  },
  {
    title: '商务对接人',
    dataIndex: 'busLinkman',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '商务对接人电话',
    dataIndex: 'busPhone',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '邮寄地址',
    dataIndex: 'postAddr',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '项目相关人员',
    dataIndex: 'teamLeader',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '客户名称',
        name: 'projectName',
        component: <Input placeholder="请输入客户名称" />,
      },
    ],
  },
];

export default {
  namespace: 'customer',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    selectListData: [], // 下拉搜索
    userList: [], // 所有用户下拉数据
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(customerPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(customerInsertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(customerDel, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *selectList({ payload }, { call, put }) {
      const res = yield call(selectList, payload);
      yield put({
        type: 'selectList_r',
        payload: res,
      });
    },
    *userList({ payload }, { call, put }) {
      const res = yield call(userList, payload);
      yield put({
        type: 'userList_r',
        payload: res,
      });
    },
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0 });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败！');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '删除失败！');
      }
      return {
        ...state,
      };
    },
    // 处理搜索下垃
    selectList_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          selectListData: payload.result,
        };
      } else {
        message.error('获取客户下拉框数据失败！');
        return {
          ...state,
        };
      }
    },
    // 处理用户下拉
    userList_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          userList: payload.result,
        };
      } else {
        message.error('获取客户下拉框数据失败！');
        return {
          ...state,
        };
      }
    },
  },
};
